//
//  JXPOIDetailView.m
//  JXAddressApp
//
//  Created by zhhw_home on 2018/11/4.
//  Copyright © 2018年 zhhw_home. All rights reserved.
//

#import "JXPOIDetailView.h"
#import "UIColor+QMUI.h"
#import "Masonry.h"
#import "JXHomeActionView.h"
#import "QMUICommonDefines.h"
#import "JXHomePhotoView.h"
#import "JXHomeDescView.h"
#import "UIView+QMUI.h"

@interface JXPOIDetailView()
<
JXHomePhotoViewDelegate
>

@property (nonatomic, strong)  UIView   *headerView;

@property (nonatomic, strong)   UILabel *titleLabel;
@property (nonatomic, strong)   UILabel *typeLabel;
@property (nonatomic, strong)   UILabel *tagLabel;

@property (nonatomic, strong)   UIButton *naviButton;

@property (nonatomic, strong)   UIScrollView    *scrollView;

@property (nonatomic, strong)   JXHomeActionView    *actionView;
@property (nonatomic, strong)   JXHomePhotoView    *photoView;
@property (nonatomic, strong)   JXHomeDescView     *descView;

@property (nonatomic, assign)   BOOL expand;
@property (nonatomic, strong)   NSDictionary    *data;

@end

@implementation JXPOIDetailView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _flag = 0;
        [self initRootView];
        
        _expand = NO;
    }
    
    return self;
}


- (instancetype)initWithFrame:(CGRect)frame flag:(NSInteger)flag
{
    if (self = [super initWithFrame:frame]) {
        _flag = flag;
        [self initRootView];
        
        _expand = NO;
    }
    
    return self;
}

- (void)initRootView
{
    self.backgroundColor = [UIColor whiteColor];
    _headerView = [UIView new];
    [self addSubview:_headerView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showMore)];
    [_headerView addGestureRecognizer:tapGesture];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont systemFontOfSize:16];
    _titleLabel.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [_headerView addSubview:_titleLabel];
    
    _typeLabel = [[UILabel alloc] init];
    _typeLabel.font = [UIFont systemFontOfSize:13];
    _typeLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
    _typeLabel.textAlignment = NSTextAlignmentLeft;
    [_headerView addSubview:_typeLabel];
    
    _tagLabel = [[UILabel alloc] init];
    _tagLabel.font = [UIFont systemFontOfSize:13];
    _tagLabel.textColor = [UIColor qmui_colorWithHexString:@"#999999"];
    _tagLabel.textAlignment = NSTextAlignmentLeft;
    _tagLabel.text = @"(普查点)";
    [_headerView addSubview:_tagLabel];
    
    _naviButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_naviButton setImage:[UIImage imageNamed:@"home_navi"] forState:UIControlStateNormal];
    [_naviButton addTarget:self action:@selector(naviAction) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:_naviButton];
    
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self);
        make.height.mas_equalTo(70);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(self).offset(10);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(250);
    }];
    
    [_typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(10);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(15);
        make.height.mas_equalTo(15);
    }];
    
    [_tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.typeLabel.mas_right).offset(10);
        make.top.mas_equalTo(self.typeLabel);
        make.height.mas_equalTo(15);
    }];
    
    [_naviButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-10);
        make.centerY.mas_equalTo(self.headerView.mas_centerY);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(40);
    }];
    
    if (_flag == 1) {
        _naviButton.hidden = YES;
    }
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, _flag==1?(self.frame.size.height-70):150)];
    [self addSubview:_scrollView];
    
    __weak typeof(self) weakSelf = self;
    _actionView = [[JXHomeActionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    _actionView.editorBlock = ^{
        __strong typeof(self) strongSelf = weakSelf;
        if ([strongSelf.delegate respondsToSelector:@selector(editorAction:)]) {
            [strongSelf.delegate editorAction:strongSelf.data];
        }
    };
    [_scrollView addSubview:_actionView];
    if (@available(iOS 11.0, *)) {
        _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    _photoView = [[JXHomePhotoView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 40)];
    _photoView.delegate = self;
    [_scrollView addSubview:_photoView];
    
    _descView = [[JXHomeDescView alloc] initWithFrame:CGRectMake(0, 40+_photoView.bounds.size.height, SCREEN_WIDTH, 40)];
    [_scrollView addSubview:_descView];
    
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, 40+_photoView.qmui_height+40);
}

- (void)naviAction
{
    if ([_delegate respondsToSelector:@selector(startNavigation:)]) {
        [_delegate startNavigation:_data];
    }
}

- (void)updateInfo:(NSDictionary*)info
{
    _titleLabel.text = info[@"place"][@"name"];
    
    NSArray *list = info[@"classes"];
    NSDictionary *item = [list firstObject];
    NSString *typeInfo = item?item[@"dmlb"]:@"";
    
    _typeLabel.text = typeInfo;
    _descView.textView.text = info[@"place"][@"description"];
    _actionView.code = info[@"place"][@"code"];
    
    _descView.qmui_height = [_descView getHeight]+20.f;
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, 40+_photoView.qmui_height+_descView.qmui_height);
    
    _data = info;
}

- (void)updateImages:(NSArray*)images
{
    [_photoView setImages:images];
}

- (void)showMore
{
    if (_flag == 1) {
        return;
    }
    
    CGFloat tabbarHeight = IS_NOTCHED_SCREEN?82:48;
    
    if (!_expand) {
        [UIView animateWithDuration:0.25 animations:^{
            CGRect newRect = CGRectMake(self.qmui_left, self.frame.origin.y+70-self.frame.size.height, self.qmui_width, self.qmui_height);
            self.frame = newRect;
        }];
    }
    else {
        [UIView animateWithDuration:0.25 animations:^{
            CGRect newRect = CGRectMake(self.qmui_left, self.frame.origin.y+self.frame.size.height-70, self.qmui_width, self.qmui_height);
            self.frame = newRect;
        }];
    }
    
    _expand = !_expand;
}

- (void)imageClick:(NSInteger)index view:(UIButton*)view
{
    if ([_delegate respondsToSelector:@selector(imageClick:view:)]) {
        [_delegate imageClick:index view:view];
    }
}

@end
